//
//  BaseInfoVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/13.
//  Copyright © 2020 tym. All rights reserved.
//

#import "BaseInfoVC.h"
#import "RoomModel.h"
#import "WHToast.h"
#import "MessageHistoryVC.h"
#import "EquipmentNameVC.h"
#import "EquipmentService.h"
#import  "RoomSeleView.h"
#import "EquipmentModel.h"
#import "FamilyRoomService.h"

@interface BaseInfoVC ()

@property(nonatomic,strong) FamilyRoomService *roomService;
@property(nonatomic,strong) EquipmentService *equipSer;
@property(nonatomic,strong) EquipmentModel *equModel;
@property(nonatomic,copy) NSMutableArray *roomsArray;
@property(nonatomic,weak) IBOutlet UILabel  *nameLab;
@property(nonatomic,weak) IBOutlet UIImageView *rihgtImage;
@property(nonatomic,weak) IBOutlet NSLayoutConstraint *rightLen;
@property(nonatomic,weak) IBOutlet UILabel  *roomLab;
@property(nonatomic,weak) IBOutlet UILabel  *modelLab;
@property(nonatomic,weak) IBOutlet UILabel  *devIDLab;
@property(nonatomic,weak) IBOutlet UILabel  *yjLab;
@property(nonatomic,weak) IBOutlet UILabel  *rjLab;

@property(nonatomic,assign) NSInteger roomIndex;
@end

@implementation BaseInfoVC

-(instancetype)initWithModel:(EquipmentModel*)model{
    self = [super init];
    self.equModel = model;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"基本信息"];
    if ([UserTool getCurrentFamily].familyRole == 2) {
        self.rihgtImage.hidden = YES;
        self.rightLen.constant = -5;
    }
    
    [self getEquipmentDetail];
}

-(EquipmentService*)equipSer{
    if (_equipSer == nil) {
        _equipSer = [[EquipmentService alloc] init];
    }
    return _equipSer;
}

-(NSMutableArray*)roomsArray{
    if (_roomsArray == nil) {
        _roomsArray = [[NSMutableArray alloc] init];
    }
    return _roomsArray;
}

-(FamilyRoomService*)roomService{
    if (_roomService == nil) {
        _roomService = [[FamilyRoomService alloc] init];
    }
    return _roomService;
}

-(void)getEquipmentDetail{
    TYMWeak(self);
    [self.equipSer getEquipmentinfo:self.equModel.equipmentId completion:^(NSError * _Nonnull error, EquipmentModel * _Nonnull equipmentModel) {
        weakself.equModel = equipmentModel;
        [weakself updateView];
        [weakself getAllRooms];
    }];
}

-(void)getAllRooms{
    TYMWeak(self);
    [MBProgressHUD wj_showActivityLoading:@"" toView:self.view];
    [self.roomService getFindfamilyroomfamilyId:[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse * _Nonnull response, NSArray * _Nonnull roosArry) {
        [MBProgressHUD wj_hideHUDForView:self.view];
        if (response.message) {
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }else{
            [weakself.roomsArray addObjectsFromArray:roosArry];
            for (int i=0; i<weakself.roomsArray.count; i++) {
                RoomModel *model = weakself.roomsArray[i];
                if (model.id == weakself.equModel.familyRoomId ) {
                    self.roomIndex = i;
                    break;
                }
            }
        }
    }];
}

-(void)updateView{
    self.nameLab.text = self.equModel.equipmentName;
    self.roomLab.text = self.equModel.roomName;
    self.modelLab.text = self.equModel.model;
    self.devIDLab.text = self.equModel.deviceId;
    self.yjLab.text = self.equModel.equipmentVersions;
    self.rjLab.text = self.equModel.softwareVersions;
}

-(void)setRoomNameIndex:(NSInteger)index{
    _roomIndex = index;
    
    RoomModel *roomModel = self.roomsArray[index];
       [self.equipSer getMoveEquipment:@[@(self.equModel.equipmentId)] familyRoomId:roomModel.id completion:^(LMJBaseResponse * _Nonnull response) {
           if (response.message) {
               [WHToast showMessage:response.message duration:2 finishHandler:nil];
           }else{
               [WHToast showMessage:@"保存成功" duration:2 finishHandler:nil];
               self.roomLab.text = roomModel.roomName;
           }
       }];
}

-(IBAction)changeNameAction:(id)sender{
    EquipmentNameVC *nameVC = [[EquipmentNameVC alloc] init];
    nameVC.equipMentID = self.equModel.equipmentId;
    nameVC.nameStr = self.nameLab.text;
    nameVC.equipmentNameBlock = ^(NSString * _Nonnull equipName) {
        self.nameLab.text = equipName;
    };
    [self.navigationController pushViewController:nameVC animated:YES];
}

-(IBAction)changRoomAction:(id)sender{
    
    NSMutableArray *roomNames = [[NSMutableArray alloc] init];
    for (RoomModel *model in self.roomsArray) {
        [roomNames addObject:model.roomName];
    }
    RoomSeleView *pop = [[RoomSeleView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    pop.data = roomNames;
    pop.index = @(self.roomIndex) ;
    TYMWeak(self);
    pop.seleRoomBackIndex = ^(NSInteger index) {
        [weakself setRoomNameIndex:index];
    };
    [pop viewShow];
}

-(IBAction)showEquipmentLogAction:(id)sender{
    
    MessageHistoryVC *historyVC = [[MessageHistoryVC alloc] initWithModel:self.equModel];
    [self.navigationController pushViewController:historyVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
